package com.baizhi.controller;

import com.baizhi.entity.Book;
import com.baizhi.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    //展示所有书籍
    @RequestMapping("/show")
    public String show(@RequestParam(defaultValue="1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize, Map map){
        List<Book> books = bookService.showBooks(pageNum, pageSize);
        PageInfo<Book> pageInfo = new PageInfo<Book>(books, 5);
        int[] pages = pageInfo.getNavigatepageNums();
        map.put("books",books);
        map.put("pages",pages);
        return "forward:/show.jsp";
    }
    //添加书籍
    @RequestMapping("/add")
    public String add(Book book){
        bookService.addBook(book);
        return "redirect:/book/show.do";
    }
    @RequestMapping("/delete")
    //删除书籍
    public String delete(Integer bookId){
        bookService.removeBookById(bookId);
        return "forward:/book/show.do";
    }
    //修改书籍信息
    @RequestMapping("/showOne")
    public String showOne(Integer bookId,Map map){
        System.out.println("bookId = " + bookId);
        Book book = bookService.showBookById(bookId);
        map.put("book",book);
        return "forward:/update.jsp";
    }
    @RequestMapping("/update")
    public String update(Book book){
        System.out.println("book = " + book);
        bookService.updateBook(book);
        return "redirect:/book/show.do";
    }
}
